/**
 *@Title: SpecialClaimController.java  
 * @author: 胡彪
 * @date: 2013-5-30 下午01:47:40
 * @Description:
 * @version: V1.0   
 */
package com.imonsoft.sqm.web.business.supplier_quality;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.imonsoft.common.constant.CCommon;
import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.exception.SystemException;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.common.httpModel.ReturnMessage;
import com.imonsoft.common.util.AjaxUtil;
import com.imonsoft.common.util.JsonUtil;
import com.imonsoft.sqm.pojo.basedata.Supplier;
import com.imonsoft.sqm.pojo.business.supplier_quality.Special_Claim;
import com.imonsoft.sqm.service.basedata.SupplierService;
import com.imonsoft.sqm.service.business.supplier_quality.SpecialClaimService;
import com.imonsoft.sqm.vo.business.supplier_quality.SpecialClaimVo;

/**
 *
 *
 */
@Controller
@RequestMapping("/business/supplier_quality/special_claim")
public class SpecialClaimController {
	protected final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private SpecialClaimService specialClaimService;

	@Autowired
	private SupplierService supplierService;

	@RequestMapping("/listUI.do")
	public void listUI() {
	}

	@RequestMapping(value = "/ajaxList.do", method = RequestMethod.POST)
	public void ajaxList(HttpServletResponse response, Pager pager, Special_Claim special_Claim) {
		DataGrid<SpecialClaimVo> dataGrid = new DataGrid<SpecialClaimVo>();
		try {
			dataGrid = specialClaimService.getDataGridVoBy(special_Claim, pager);
		} catch (SystemException se) {
			logger.debug(se.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(dataGrid));
	}

	@RequestMapping(value = "/modifyUI.do")
	public void modifyUI(ModelMap model, Long id, String year) {

		if(id==null){
			new Exception(CCommon.EXCEPTION_MSG);
		} 
		if (year != null && !year.equals("undefined")) {
			SpecialClaimVo specialClaimVo = specialClaimService.getModifyData(id, year);
			specialClaimVo.setYear(year);
			model.put("specialClaimVo", specialClaimVo);
		} else {
			Supplier supplier = supplierService.get(id);
			SpecialClaimVo specialClaimVo = new SpecialClaimVo();
			specialClaimVo.setId(supplier.getId().toString());
			specialClaimVo.setCode(supplier.getCode());
			specialClaimVo.setName(supplier.getName());
			specialClaimVo.setType(supplier.getMaterialGroup());
			specialClaimVo.setYear(year);
			model.put("specialClaimVo", specialClaimVo);
		}

	}

	@RequestMapping(value = "/addUI.do")
	public void addUI(ModelMap model) {

	}

	@RequestMapping(value = "/ajaxModify.do", method = RequestMethod.POST)
	public void ajaxModify(HttpServletResponse response) {
		ReturnMessage message = new ReturnMessage();
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		try {
			String specialClaimDetail = request.getParameter("specialClaimDetail"); // 获取总目
			String specialClaim = request.getParameter("specialClaim"); // 获取细目
			specialClaimService.txUpdateOrAddSpecialClaim(specialClaimDetail, specialClaim);
			message.setSuccess(true);
			message.setMsg(ConstantSqm.OPERATE_SUCCESS);
		} catch (SystemException se) {
			logger.debug(this.getClass() + ":" + se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			logger.error(this.getClass() + ":" + e.getMessage());
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}

	@RequestMapping(value = "/ajaxAdd.do", method = RequestMethod.POST)
	public void ajaxAdd(HttpServletResponse response, Special_Claim special_Claim,BindingResult result) {
		ReturnMessage message = new ReturnMessage();
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		try {
			String specialClaimDetail = request.getParameter("specialClaimDetail"); // 获取总目
			String specialClaim = request.getParameter("specialClaim"); // 获取细目
			specialClaimService.txAddSpecialClaim(specialClaimDetail, specialClaim,message,result);
		
		} catch (SystemException se) {
			se.printStackTrace();
			logger.debug(this.getClass() + ":" + se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			logger.error(this.getClass() + ":" + e.getMessage());
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}
	
	@RequestMapping(value = "/ajaxDelete.do", method = RequestMethod.POST)
	public void ajaxDelete(HttpServletResponse response) {
		ReturnMessage message = new ReturnMessage();
		try {
			HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
			String specialClaimDetail = request.getParameter("specialClaimDetail"); // 获取总目
			specialClaimService.deleteSpecialClaim(specialClaimDetail);
			message.setSuccess(true);
			message.setMsg(ConstantSqm.OPERATE_SUCCESS);
		} catch (SystemException se) {
			logger.debug(this.getClass() + ":" + se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			logger.error(this.getClass() + ":" + e.getMessage());
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}
	
	
}
